package cn.kgc.tangcco.tcbd1011.no13.customer.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.kgc.tangcco.tcbd1011.no13.base.action.BaseServlet;
import cn.kgc.tangcco.tcbd1011.no13.base.properties.factory.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.no13.base.util.MyBeanUtils;
import cn.kgc.tangcco.tcbd1011.no13.base.util.Utils;
import cn.kgc.tangcco.tcbd1011.no13.customer.entity.Customer;
import cn.kgc.tangcco.tcbd1011.no13.customer.service.CustomerService;

/**
 * Servlet implementation class CustomerServlet
 */
@WebServlet(name="CustomerServlet",urlPatterns="/customer.action")
public class CustomerServlet extends BaseServlet {
	private static final String SUCCESS = "customer.action?methodName=doListCustomer";
    private CustomerService cService;
    public CustomerServlet() {
    	cService = (CustomerService) PropertiesFactory.getInstance(Utils.CUSTOMERSERVICE);
    }
    //查询所有
    public String doListCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = "customer/listCustomer";
		request.setAttribute("customer", this.cService.queryAllCustomer(null));
		return forward;
	}
    //去增加页面
    public String toAddCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = "customer/addCustomer";
		return forward;
	}
    //执行新增操作
    public String doAddCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = SUCCESS;
		request.setAttribute("rows", this.cService.addCustomer(MyBeanUtils.populate(Customer.class, request.getParameterMap())));
		return forward;
	}
    //查询详细信息
    public String detailCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = "customer/detailCustomer";
		request.setAttribute("customer", cService.detailCustomer(Utils.parseInt(request.getParameter("customerId"))));
		return forward;
	}
    //去修改 页面
    public String toUpdateCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = "customer/updateCustomer";
		request.setAttribute("customer", cService.detailCustomer(Utils.parseInt(request.getParameter("customerId"))));
		return forward;
	}
    //执行修改操作
    public String doUpdateCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = SUCCESS;
		request.setAttribute("rows", cService.updateCustomer(MyBeanUtils.populate(Customer.class, request.getParameterMap())));
		return forward;
	}
    //删除
    public String doDeleteCustomer(HttpServletRequest request,HttpServletResponse response){
		String forward = SUCCESS;
		request.setAttribute("rows", cService.deleteCustomer(Utils.parseInt(request.getParameter("customerId"))));
		return forward;
	}
    
    
}
